package com.groupseven.happycampus.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupseven.happycampus.common.pojo.ErrandAddress;
import com.groupseven.happycampus.common.pojo.ErrandOrder;
import com.groupseven.happycampus.common.vo.LayuiPageVo;
import com.groupseven.happycampus.mapper.ErrandMapper;
import com.groupseven.happycampus.service.ErrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("/errandService")
public class ErrandServiceImpl implements ErrandService {

    @Autowired
    private ErrandMapper errandMapper;

    @Override
    public int addAddress(ErrandAddress address) {
        int i = errandMapper.addAddress(address);
        return i;
    }

    @Override
    public int deleteAddress(Integer id) {
        int i = errandMapper.deleteAddress(id);
        return i;
    }

    @Override
    public int updateAddress(ErrandAddress address) {
        int i = errandMapper.updateAddress(address);
        return i;
    }

    @Override
    public LayuiPageVo<ErrandAddress> addressPageData(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        PageHelper.startPage(page,limit);

        List<ErrandAddress> addresses = errandMapper.selectAddressByParams(params);

        PageInfo<ErrandAddress> addressPageInfo = new PageInfo<>(addresses);

        LayuiPageVo<ErrandAddress> addressLayuiPageVo = new LayuiPageVo<>();
        addressLayuiPageVo.setCode(0);
        addressLayuiPageVo.setMsg("分页列表数据");
        addressLayuiPageVo.setCount(addressPageInfo.getTotal());
        addressLayuiPageVo.setData(addressPageInfo.getList());

        return addressLayuiPageVo;
    }

    @Override
    public int addOrder(ErrandOrder order) {
        int i = errandMapper.addOrder(order);
        return i;
    }

    @Override
    public int deleteOrder(Integer id) {
        int i = errandMapper.deleteOrder(id);
        return i;
    }

    @Override
    public int updateOrder(ErrandOrder order) {
        int i = errandMapper.updateOrder(order);
        return i;
    }

    @Override
    public LayuiPageVo<ErrandOrder> orderPageData(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        PageHelper.startPage(page,limit);

        List<ErrandOrder> orders = errandMapper.selectOrderByParams(params);

        orders.stream().forEach(System.out::println);
        PageInfo<ErrandOrder> orderPageInfo = new PageInfo<>(orders);

        LayuiPageVo<ErrandOrder> orderLayuiPageVo = new LayuiPageVo<>();
        orderLayuiPageVo.setCode(0);
        orderLayuiPageVo.setMsg("分页列表数据");
        orderLayuiPageVo.setCount(orderPageInfo.getTotal());
        orderLayuiPageVo.setData(orderPageInfo.getList());

        return orderLayuiPageVo;
    }

    @Override
    public int deleteErrandsByIds(String ids) {
        String[] arrId = ids.split(",");
        return errandMapper.deleteErrandsByIds(arrId);
    }

    @Override
    public List<ErrandOrder> showAddress(String telephone) {
        return errandMapper.showAddress(telephone);
    }
}
